<?php
global $_RQ,$_AR;
$_RQ['op']=$_RQ['op']?$_RQ['op']:'list';
if($_RQ['op']=='list'){
	$pindex = max(1, intval($_RQ['page']));
	$psize = 10;
	$params=array('merchid'=>$merchid,'uniacid'=>$_SESSION['uniacid']);
	if(!empty($_RQ['title'])){
		$params['title like']='%'.trim($_RQ['title']).'%';
		$return['params']['title']=$_RQ['title'];
	}
	if(!empty($_RQ['status'])){
		$params['status']=trim($_RQ['status']);
		$return['params']['status']=intval($_RQ['status']);
	}
    $orderby='id desc';
	$total='';
	$return['list']=pdo_getslice('jy_aihire_xieyitpl',$params,array($pindex,$psize),$total,$fields,'',$orderby);
	$totalpage=ceil($total/$psize);
	$totalpage=intval($totalpage);
	$return['totalpage'] =$totalpage;
	$return['params']['page'] =$pindex;
	$return['status']=$this->xieyi_status;
	$return['isdefault']=array('0'=>'否','1'=>'是');
	$htmls=array(
		'type'=>'list',
		'tabs'=>array(
			array('op'=>'list','title'=>'企业协议模板'),
			array('op'=>'edit','title'=>'添加协议模板'),
		),
		'list'=>array('data'=>'list','list'=>array(	
			array('name'=>'title','title'=>'协议名称','type'=>'string'),
			array('name'=>'readnum','title'=>'阅读人数','type'=>'string'),
			array('name'=>'checknum','title'=>'确认人数','type'=>'string'),
			//array('name'=>'workerid','title'=>'创建人','type'=>'select','options'=>'workers'),
			array('name'=>'createtime','title'=>'创建时间','type'=>'time'),
			array('name'=>'status','title'=>'状态','type'=>'switch','set'=>'set'),				
		),'action'=>array(
			array('type'=>'modal','op'=>'look','title'=>'查看'),
		)),
		'search'=>array(
			array('name'=>'title','width'=>10,'title'=>'协议名称','type'=>'string'),
			array('name'=>'status','width'=>10,'title'=>'状态','type'=>'radio','options'=>'status'),
		)
	);
	$return['htmls']=$htmls;
	exi($return);
}
if($_RQ['op']=='look'){
	$id=intval($_RQ['id']);
	if($id){
		$item=pdo_get('jy_aihire_xieyitpl',array('id'=>$id));
	}else{
		$item=array();
	}
    $return['item']=$item;
	$htmls=array(
		'type'=>'look',
		'look'=>array(
			array('name'=>'title','title'=>'标题','type'=>'string'),
			array('name'=>'content','title'=>'内容','type'=>'richtext'),		
		)
	);
	$return['htmls']=$htmls;
	exi($return);
}
if($_RQ['op']=='edit'){
	$id=intval($_RQ['id']);
	if($id){
		$item=pdo_get('jy_aihire_xieyitpl',array('id'=>$id));
	}else{
		$item=array();
	}
    $return['item']=$item;
	$htmls=array(
		'type'=>'edit',
		'post'=>'post',
		'desc'=>'协议设置',
		'tabs'=>array(
			array('op'=>'list','title'=>'协议管理'),
			array('op'=>'edit','title'=>'添加协议')
		),
		'edit'=>array(
			array('name'=>'title','title'=>'标题','type'=>'string'),
			array('name'=>'content','title'=>'内容','type'=>'richtext'),		
		)
	);
	$return['htmls']=$htmls;
	exi($return);
}
if($_RQ['op']=='post'){
	$postdata=get_postdata();
	$data=array_fields(array('title','content'),$postdata);
	$id=intval($postdata['id']);
	if($id){		
		pdo_update('jy_aihire_xieyitpl',$data,array('id'=>$id));
		exi('设置成功！','','goback');
	}else{
		$data['uniacid']=$_SESSION['uniacid'];
		$data['merchid']=$merchid;
		$data['createtime']=time();
		pdo_insert('jy_aihire_xieyitpl',$data);
		exi('添加成功！','','goback');
	}
}
if($_RQ['op']=='set'){
	$id=intval($_RQ['id']);
	if(!$id){
		exi('参数错误！','error');
	}
	$update=array();
	$post=get_postdata();
	if(isset($post['status'])){
		$update['status']=intval($post['status']);
	}
	if($update){
		pdo_update('jy_aihire_xieyitpl',$update,array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
	}
	exi('操作成功！');
}
if($_RQ['op']=='del'){

	$id=intval($_RQ['id']);
	if($id){
		pdo_delete('jy_aihire_xieyitpl',array('id'=>$id));	
	}
	$del=trim($_RQ['del'],';');
	if($del){
		$del=explode(';',$del);
		foreach($del as $id){
			$id=intval($id);
			if($id){
				pdo_delete('jy_aihire_xieyitpl',array('id'=>$id));
			}
		}
	}
	exi('删除成功！');
}
?>